﻿import java.util.*;

/*最初的繼承檔 設定需要的函式(速度 位置等) 讓其他馬繼承*/
abstract class oldhor{					//老馬
	protected String name;				//馬名
	protected static int speed;			//速度
	protected static int move = 0 ;		//位移
	public oldhor (String horsename){
		this.name = horsename;
	}	
	abstract int finil();				//確定結束用
	abstract int time();				//排名用
}				

/*繼承檔一 設置賽馬時的加減速及特殊情況 
     三例外：1.一開始跑錯跑道 2.大幅減速 3.倒退
     其餘有加速 減速 維持三種情況
     當有低於最低限速時不會再減速 */

class  smallhor extends oldhor{		//小馬繼承老馬
	static int SmallTurn = 0 ;
	static int SmallSpeed = speed;
	static int SmallMove = move;
	public smallhor(String SmallName, int StartException){			//取名
		super(SmallName); 
		try{
			if(SmallMove == 0){				//起跑
				if(StartException == 1 && SmallTurn == 0){			//跑錯跑道的情況
					throw new ExceptionFirst();
				}
				else{												//順利起跑
					SmallSpeed = 50;	
					SmallMove += SmallSpeed;
					System.out.printf("%S起跑，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
				}
			}
			else if(SmallMove > 300){
				System.out.printf("%s休息中\n",SmallName);		//休息
			}
			else{										//過彎
				if(SmallMove < 100 && SmallMove+SmallSpeed > 100){
					SmallSpeed = 35;
					SmallMove += SmallSpeed;
					System.out.printf("%s過彎，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
				}
				else if(SmallMove < 200 && SmallMove+SmallSpeed > 200){				//加減速
					SmallSpeed = 35;
					SmallMove += SmallSpeed;
					System.out.printf("%s過彎，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
				}
				else{
					int seed;							//加減速變數參數
					if(SmallSpeed <= 20){
						seed = (int)(Math.random()*2); //0~1的隨機亂數(取整數)
					}
					else{
						seed = (int)(Math.random()*5); //0~2的隨機亂數(取整數)
					}
					
					switch(seed){
					case 0:{
						SmallMove += SmallSpeed;
						System.out.printf("%s維持速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
						break;
					}
						
					case 1:{
						SmallSpeed+= 30;
						SmallMove += SmallSpeed;
						System.out.printf("%s加速30，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
						break;
					}
					case 2:{
						SmallSpeed -= 15;
						SmallMove += SmallSpeed;
						System.out.printf("%s減速15	，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
						break;
					}
					case 3:{
						throw new ExceptionSecond();
					}
					case 4:	{
						throw new ExceptionThird();
					}
					}
				}
			}
		}
		catch (ExceptionFirst e){
			System.out.printf("%S跑錯跑道，須重新起跑，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);
		}
		catch (ExceptionSecond e){
			if (SmallSpeed<60){
				SmallSpeed = 0;
			}
			else {
				SmallSpeed -= 50;
			}
			SmallMove += SmallSpeed;
			System.out.printf("%s愚蠢的自己左前腳拌到右後腳，速度大幅變慢，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);		
		}
		catch (ExceptionThird e){
			SmallSpeed = 0;
			SmallMove -= 50;
			System.out.printf("%s看到了旁邊的母馬，忘記自己在比賽，後退了50單位，速度%d，位置%d\n",SmallName,SmallSpeed,SmallMove);}
	}
	
	
	public int finil(){				//跑完
		if (SmallMove>=300 ){
			return 0;
		}
		else {
			return 1;
		}	 
	}
	
	
	public int time(){				//排名	
		if (SmallMove>=300){
			return SmallTurn;
		} 
		else{
			SmallTurn++;
			return 0;
		}
	}
}

/*繼承擋二 狀況同小馬 速度設置不同*/
class  bighor extends oldhor{		//大馬同小馬
	static int BigTurn = 0 ;
	static int BigSpeed = speed;
	static int BigMove = move;
	public bighor(String BigName, int StartException){
		super(BigName); 
		try{
			if(BigMove == 0){	
				if(StartException == 1 && BigTurn == 0){
					throw new ExceptionFirst();
				}	
				else{
					BigSpeed = 65;
					BigMove += BigSpeed;
					System.out.printf("%S起跑，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
				}
			}
			else if(BigMove > 300){
				System.out.printf("%s休息中\n",BigName);
			}
			else{
				if(BigMove < 100 && BigMove+BigSpeed > 100){
					BigSpeed = 40;
					BigMove += BigSpeed;
					System.out.printf("%s過彎，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
				}
				else if( BigMove < 200 && BigMove+BigSpeed > 200 ){
					BigSpeed = 40;
					BigMove += BigSpeed;
					System.out.printf("%s過彎，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
				}
				else{
					int seed ;
					if( BigSpeed <= 30){
						seed = (int)(Math.random()*2);	//0~1的隨機亂數(取整數)
					} 
					else{
						seed = (int)(Math.random()*5);	//0~2的隨機亂數(取整數)
					} 
					switch(seed){
					case 0:{
						BigMove += BigSpeed;
						System.out.printf("%s維持速度%d，位置%d\n",BigName,BigSpeed,BigMove);
						break;
					}
					case 1:{
						BigSpeed+= 40;
						BigMove += BigSpeed;
						System.out.printf("%s加速40，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
						break;
					}						
					case 2:{
						BigSpeed -= 25;
						BigMove += BigSpeed;
						System.out.printf("%s減速25，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
						break;
					}
					case 3:{
						throw new ExceptionSecond();
					}
					case 4:{
						throw new ExceptionThird();
					}
					}
				}
			}
		}
		catch (ExceptionFirst e){
			System.out.printf("%S跑錯跑道，須重新起跑，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
		}
		catch (ExceptionSecond e){
			if (BigSpeed<100){
				BigSpeed = 0;
			}
			else{
				BigSpeed -= 80;
			}
			BigMove += BigSpeed;
			System.out.printf("%s愚蠢的自己左前腳拌到右後腳，速度大幅變慢，速度%d，位置%d\n",BigName,BigSpeed,BigMove);
		}
		catch (ExceptionThird e){
			BigSpeed = 0;
			BigMove -= 50;
			System.out.printf("%s看到了旁邊的母馬，忘記自己在比賽，後退了50單位，速度%d，位置%d\n",BigName,BigSpeed,BigMove);}
	}
	
	
	public int finil(){
		if( BigMove>=300 ){
			return 0;
		} 
		else{
			return 1;
		}
	}
	
	
	public int time(){	
		if(BigMove>=300){
			return BigTurn;
		} 
		else{
			BigTurn++;
			return 0;
		}
	}
}

/*讀馬名及輸出資訊*/
public class horse{
	public static void main(String[] args){		//取馬名
		System.out.println("歡迎來到娛樂馬場");
		System.out.println("總長300");
		System.out.println("100,200處為彎道");
		String []horse = new String[2];
		System.out.println("請輸入第一隻馬名:");
		Scanner horse1 = new Scanner(System.in);
		horse[0] = horse1.next();
		System.out.println("請輸入第二隻馬名:");
		Scanner horse2 = new Scanner(System.in);
		horse[1] = horse2.next();
		run(horse[0],horse[1]);}
	public static  int run(String SmallName,String BigName){				//跑馬
		int count = 1;
		int SmallFinal,BigFinal;
		int turn[] = new int[2];
		int []startex = new int[2];
		int oneex = (int)(Math.random()*2); //0~1的隨機亂數(取整數)一開始有一隻馬會跑錯跑道
		startex[oneex] = 1;
		
		do{
			System.out.printf("第%d階段\n",count);
			smallhor h1 = new smallhor(SmallName,startex[0]);
			bighor h2 = new bighor(BigName,startex[1]);
			SmallFinal = h1.finil();
			BigFinal = h2.finil(); 
			turn[0] = h1.time(); 
			turn[1] = h2.time(); 
			count++;}while(SmallFinal != 0 || BigFinal != 0 );
			System.out.printf("%s在第%d階段完成\n",SmallName,turn[0]);
			System.out.printf("%s在第%d階段完成\n",BigName,turn[1]);
			return 0;
	}
}